package com.buka.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buka.api.CouponApi;
import com.buka.common.BizCodeEnum;
import com.buka.dao.UserDao;
import com.buka.dto.UserDto;
import com.buka.entity.User;
import com.buka.request.NewUserCouponRequest;
import com.buka.service.UserService;
import com.buka.util.CommonUtil;
import com.buka.util.JWTUtil;
import com.buka.util.JsonData;
import com.buka.vo.LoginUserVo;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2025-07-30 16:34:06
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    MailService mailService;

    @Resource
    CouponApi couponApi;

    @Override
    @GlobalTransactional
    public JsonData register(UserDto userDto) {
        //校验 邮箱验证码
        if (!mailService.checkMailCode(userDto.getMail(), userDto.getCode())) {
            return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
        }

        //执行注册
        User user = new User();
        BeanUtils.copyProperties(userDto, user);

        //密码加密
        String pwd = user.getPwd();
        //生成随机码  加盐算法，必须要有前缀 $1$
        String randomString = "$1$" + CommonUtil.getRandomString(8);
        String newPasswd = Md5Crypt.md5Crypt(pwd.getBytes(), randomString);

        user.setSecret(randomString);
        user.setPwd(newPasswd);
        save(user);
//        注册成功领取新人优惠券
        NewUserCouponRequest userCouponRequest = new NewUserCouponRequest();
        userCouponRequest.setUserId(user.getId());

        //openFeign传递对象
        couponApi.newUserCoupon(userCouponRequest);

        return JsonData.buildSuccess();
    }

    @Override
    public JsonData login(LoginUserVo loginUserVo, HttpServletRequest request) {
        //首先使用邮箱查询用户是否存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getMail, loginUserVo.getMail());
        User one = getOne(queryWrapper);
        if (ObjectUtil.isEmpty(one)) {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_UNREGISTER);
        }

        //查到了得到用户的信息 包含了密码，盐
        String secret = one.getSecret();
        String loginPwd = loginUserVo.getPwd();
        String regPwd = one.getPwd();
        // 使用数据库中的盐与当前登录传递的密码 生成新的密文和数据库中密码匹配
        String code = Md5Crypt.md5Crypt(loginPwd.getBytes(), secret);
        if (StrUtil.equals(code, regPwd)) {
            //登录成功返回对象信息不安全  JWT
            BeanUtils.copyProperties(one,loginUserVo);

            String jwt = JWTUtil.createJwtWithDevice(request,loginUserVo);
            return JsonData.buildSuccess(jwt);
        } else {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
        }
    }
}

